local skel = fk.CreateSkill {
  name = "rmt__zhungu",
}

Fk:loadTranslationTable{
  ["rmt__zhungu"] = "谆古",
  [":rmt__zhungu"] = "每轮各限一次，当你受到伤害后，失去最后手牌后，手牌数成为全场最大后，你可以选择回复1点体力或摸一张牌，然后若体力值/手牌数小于你上次触发的任一另外时机的此值，再执行一次。",

  ["@rmt__zhungu_damage"] = "谆伤",
  ["@rmt__zhungu_last"] = "谆失",
  ["@rmt__zhungu_max"] = "谆大",
  ["#rmt__zhungu-hp"] = "调整体力值至 %arg",
  ["#rmt__zhungu-hand"] = "调整手牌数至 %arg",
  ["#rmt__zhungu-choice"] = "谆古：你可以选择回复1点体力或摸一张牌",

  ["$rmt__zhungu1"] = "庶有疑嫡之子，朝有疑相之臣，国家乱矣。",
  ["$rmt__zhungu2"] = "父没，三年无改父之道，圣人教也。",
}

local on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    local choice = cost_data.choice
    if choice == "draw1" then
      player:drawCards(1, skel.name)
    else
      room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
    end
    if player.dead then return end
    local extra
    local list = {"rmt__zhungu_max", "rmt__zhungu_last", "rmt__zhungu_damage"}
    for _, name in ipairs(list) do
      if name ~= cost_data.event then
        local mark = player:getMark("@"..name)
        if mark ~= 0 then
          local hand, hp = table.unpack(table.map(mark, function(e) return tonumber(string.split(e, ":")[2]) end))
          if (choice == "draw1" and player:getHandcardNum() < hand) or (choice == "recover" and player.hp < hp) then
            extra = true
            break
          end
        end
      end
    end
    if extra then
      if choice == "draw1" then
        player:drawCards(1, skel.name)
      else
        room:recover { num = 1, skillName = skel.name, who = player, recoverBy = player }
      end
    end
  end

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return data.extra_data and
      ((data.extra_data.rmt__zhunguLast and player:getMark("rmt__zhungu_last-round") == 0)
      or (data.extra_data.rmt__zhunguMax and player:getMark("rmt__zhungu_max-round") == 0))
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local theEvent
    if data.extra_data.rmt__zhunguLast then
      theEvent = "rmt__zhungu_last"
    elseif data.extra_data.rmt__zhunguMax then
      theEvent = "rmt__zhungu_max"
    end
    if not theEvent then return end
    local choices = {"draw1", "Cancel"}
    if player:isWounded() then
      table.insert(choices, 2, "recover")
    end
    local choice = room:askToChoice(player, { choices = choices, skill_name = skel.name, prompt = "#rmt__zhungu-choice"})
    if choice ~= "Cancel" then
      room:setPlayerMark(player, theEvent.."-round", 1)
      event:setCostData(self, {choice = choice, event = theEvent})
      return true
    end
  end,
  on_use = on_use,
})

skel:addEffect(fk.Damaged, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return target == player and player:getMark("rmt__zhungu_damage-round") == 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local theEvent = "rmt__zhungu_damage"
    local choices = {"draw1", "Cancel"}
    if player:isWounded() then
      table.insert(choices, 2, "recover")
    end
    local choice = room:askToChoice(player, { choices = choices, skill_name = skel.name, prompt = "#rmt__zhungu-choice"})
    if choice ~= "Cancel" then
      room:setPlayerMark(player, theEvent.."-round", 1)
      event:setCostData(self, {choice = choice, event = theEvent})
      return true
    end
  end,
  on_use = on_use,
})

skel:addEffect(fk.Damaged, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true) and target == player
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local hand, hp = player:getHandcardNum(), player.hp
    local record = {"牌:"..hand, "体:"..hp}
    room:setPlayerMark(player, "@rmt__zhungu_damage", record)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(skel.name, true) then
      for _, move in ipairs(data) do
        if move.toArea == Player.Hand then return true end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Player.Hand then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local hand, hp = player:getHandcardNum(), player.hp
    local record = {"牌:"..hand, "体:"..hp}
    local oldKongcheng, oldMax = player.tag["rmt__zhungu_last"], player.tag["rmt__zhungu_max"]
    local newKongcheng, newMax = player:isKongcheng(), table.every(room.alive_players, function (p)
      return player:getHandcardNum() >= p:getHandcardNum()
    end)
    player.tag["rmt__zhungu_last"] = newKongcheng
    player.tag["rmt__zhungu_max"] = newMax
    if newKongcheng and oldKongcheng == false then
      data.extra_data = data.extra_data or {}
      data.extra_data.rmt__zhunguLast = true
      room:setPlayerMark(player, "@rmt__zhungu_last", record)
    end
    if newMax and oldMax == false then
      data.extra_data = data.extra_data or {}
      data.extra_data.rmt__zhunguMax = true
      room:setPlayerMark(player, "@rmt__zhungu_max", record)
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local newKongcheng, newMax = player:isKongcheng(), table.every(room.alive_players, function (p)
    return player:getHandcardNum() >= p:getHandcardNum()
  end)
  player.tag["rmt__zhungu_last"] = newKongcheng
  player.tag["rmt__zhungu_max"] = newMax
end)

skel:addLoseEffect(function (self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "@rmt__zhungu_damage", 0)
  room:setPlayerMark(player, "@rmt__zhungu_last", 0)
  room:setPlayerMark(player, "@rmt__zhungu_max", 0)
end)

return skel
